﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Colors;

namespace Galaxy.AutoCAD.Tests
{
    public static class HatchTools
    {
        public static string CurrentPattern
        {
            get
            {
                return Application.GetSystemVariable("HPNAME").ToString();
            }
            set
            {
                //如果要设置的值符合填充图案名，则设置HPNAME系统变量值
                if (value.Length <= 34 && !value.Contains(" ") && !string.IsNullOrWhiteSpace(value) && value != CurrentPattern) Application.SetSystemVariable("HPNAME", value);
            }
        }
        public static void CreateHatch(this Hatch hatch, HatchPatternType patternType, string patternName, bool associative)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            hatch.SetDatabaseDefaults();//设置填充的属性为当前数据库默认值
            //设置图案填充的类型和填充图案名
            hatch.SetHatchPattern(patternType, patternName);
            db.AddToModelSpace(hatch);//将填充添加到模型空间
            //设置填充与边界是否关联
            hatch.Associative = associative ? true : false;
        }
        public static void CreateGradientHatch(this Hatch hatch, HatchGradientName gradientName, Color color1, Color color2, bool associative)
        {
            //设置渐变填充的类型所代表的字符串，用于将HatchGradientName枚举转换为对应的字符串
            string[] gradientNames = new string[] { "Linear", "Cylinder", "Invcylinder", "Spherical", "Invspherical", "Hemisperical", "InvHemisperical", "Curved", "Incurved" };
            Database db = HostApplicationServices.WorkingDatabase;
            hatch.SetDatabaseDefaults();//设置填充的默认值
            //设置渐变填充的类型和图案名称
            hatch.SetGradient(GradientPatternType.PreDefinedGradient, gradientNames[(int)gradientName]);
            //新建两个Color类对象，分别表示渐变填充的起始和结束颜色
            GradientColor gColor1 = new GradientColor(color1, 0);
            GradientColor gColor2 = new GradientColor(color2, 1);
            //设置渐变色的起始和结束颜色
            hatch.SetGradientColors(new GradientColor[] { gColor1, gColor2 });
            db.AddToModelSpace(hatch);//将填充添加到模型空间
            //设置填充与边界是否关联
            hatch.Associative = associative ? true : false;
        }
        public enum HatchGradientName
        {
            Linear,//线状渐变
            Cylinder,//圆柱状渐变
            Invcylinder,//反圆柱状渐变
            Spherical,//球状渐变
            Invspherical,//反球状渐变
            Hemisperical,//半球状渐变
            InvHemisperical,//反半球状渐变
            Curved,//抛物面状渐变
            Incurved//反抛物面状渐变
        }
    }
}
